<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$this->model('member')->upmember();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	if(!empty($_SESSION['openid'])){
		$openid = $_SESSION['openid'];
		$fansfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'openid'=>$_SESSION['openid']),'follow');
	}
	$member = pdo_get('jy_chatwenxin_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$member2['avatar']=tomedia($member2['avatar']);
	$member=array_merge($member,$member2);
	$return['openid']= $_SESSION['openid'];
	$return['member']= $member;
	$return['welcomehdps']=$return['pluginset']['welcomehdps'];
	if(count($return['pluginset']['welcomehdps'])>0 && $return['pluginset']['iswelcome']==1){
		$return['iswelcome']=$this->session('iswelcome')?0:1;
		if($return['iswelcome']){
			$this->session('iswelcome',1);
		}
	}
	if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
		$return['pluginset']['welcome'] = str_replace("\n","</br>",$return['pluginset']['welcome']);
	}
	$return['list']= array();
	if($return['basic']['bi_sale2']>0 && $_RQ['fromuser']){
		if($return['basic']['bi_sale2max']>0){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$isgetfee=pdo_getsum('core_credit_log',array('createtime >'=>$daytime,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_RQ['fromuser'],'plugin'=>'jy_chatwenxin','tag like'=>'%bi_sale2%'),'fee');
			if($isgetfee>=$return['basic']['bi_sale2max']){
				$nogetfee=1;
			}
		}
		if(!$nogetfee){
			//今天来自某人的
			$isgetfee=pdo_getsum('core_credit_log',array('createtime >'=>$daytime,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_RQ['fromuser'],'plugin'=>'jy_chatwenxin','tag'=>'bi_sale2'.$_SESSION['uid']),'fee');
		}
		if(!$nogetfee && !$isgetfee){
			model('core/member')->member_credit($_RQ['fromuser'],'credit3',$return['basic']['bi_sale2'],true,'jy_chatwenxin','每次分享赚金币','bi_sale2'.$_SESSION['uid']);
		}
	}
	$return['post']= array('msg'=>'');
	$return['sharedata']=array(
		'title'=>$return['basic']['share_title'],
		'desc'=>$return['basic']['share_desc'],
		'link'=>custom_url('jy_chatwenxin/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['basic']['share_image'],
	);
	set_html('title',$return['pluginset']['title']?:'chatAI智能聊天对话');
	exi($return);
}
if($_RQ['op']=='list'){
	$chatid=$_RQ['chatid']?:$_SESSION['chatid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$chatid,'mid'=>$_SESSION['uid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 6;
	$total = 0;
	$chatlog=pdo_getslice('jy_chatwenxin_msglog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$chatlogtime = time();
	if($chatlog){
		foreach($chatlog as $k=>&$v){
					$settime = $v['createtime'];
					if(($chatlogtime - $v['createtime']) > 600 || $k+1==count($chatlog)){
						$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
					}else{
						$v['createtime'] = '';
					}
					$chatlogtime = $settime;
					$v['msg'] = cfc('tools')->filterword($v['msg'],$setting['filterword']);
					$v['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $v['msg']);
					$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
					preg_match_all($hrefhtml,$v['msg'],$resarray);  
					if($v['type'] == 1 || $v['type'] == 2){
						foreach($resarray[0] as &$vv){
							if(!empty($vv)){
								$v['msg'] = str_replace($vv,"<a href='".$vv."'>".$vv."</a>",$v['msg']);
							}
						}
					}
					if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
						$v['answer'] = str_replace("\n","</br>",$v['answer']);
					}
		}
	}
	$return['list']= $chatlog;
	exi($return);
}
if($_RQ['op']=='gethtml'){
	$id=intval($_RQ['id']);
	$chatid=intval($_RQ['chatid']);
	$getmemberlogo=$this->session('getmemberlogo');
	$memberlogo=$this->session('memberlogo');
	$info=pdo_get('jy_chatwenxin_chatlog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$chatid,'id'=>$id));
	if($_SESSION['os']=='windows'){
		$htmltime = '<div class="message_box_time">'.$info['createtime'].'</div>';
		if($info['type'] == 1 || true){
			$html .= $htmltime;
			if($info['setmid'] != $_SESSION['uid']){
					$html .='<div class="message_box"><img class="mssage_box_pic" src="'.$getmemberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['answer'].'</p></div></div></div>';
			}else{
					$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['askmsg'].'</p></div></div></div>';
			}			
		}
	}else{
		if($info['type'] == 1 || true){
			if($info['setmid'] == $_SESSION['uid']){
				$html .='<div class="r clearfix"><img class="pic"/><p class="text">'.$info['askmsg'].'</p></div>';
			}else{
				$html .='<div class="l clearfix"><img class="pic"/><p class="text">'.$info['answer'].'</p></div>';
			}
			$html .= $htmltime;
		}
	}
	$return['html']= $html;
	exi($return);
}
if($_RQ['op']=='addlog'){
	$postdata=get_postdata();
	$chatmsg = trim($postdata['msg']);
	$logid=$this->model('kefu')->addchatlog($chatmsg);
	$res['log']=array(
		'id'=>$logid,
		'mid'=>$_SESSION['uid'],
		'chatid'=>$_SESSION['chatid'],
		'askmsg'=>$chatmsg,
	);
	exi($res);
}
if($_RQ['op']=='getmsg'){
	$logid = trim($_RQ['logid']);
	$chatmsg = trim($_RQ['msg']);
	$res['key'] = intval($_RQ['key']);
	$res['msg']=$this->model('member')->bi_up();
	if(!$res['msg']){
		$res['msg']=$this->model('model')->getmsg($chatmsg);
	}
	if($res['msg'] && $res['msg']!='fail'){
		pdo_update('jy_chatwenxin_msglog',array('answer'=>$res['msg']),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		$checkvip=$this->model('member')->checkvip('',true);
		if($checkvip!='isvip'){
			$basic=$this->plugin_setting('basic');
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_chatwenxin','tag'=>'bi_post'.$logid),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$basic['bi_post'],false,'jy_chatwenxin','提问消耗','bi_post'.$logid);
			}
		}
	}
	if($res['msg']=='fail'){
		$res['fail']=1;
		$res['msg']='';
	}
	exi($res);
}